#include "enemy.h"
#include "GameSetting.h"
#include <QPixmap>
#include <QRandomGenerator>
#include <QGraphicsScene>
#include "player.h"
#include "bullet.h"
#include "score.h"
#include "health.h"

Enemy::Enemy(QGraphicsItem *parent) : QGraphicsPixmapItem(parent)
{
    setPixmap(QPixmap(":/images/Sprites/Enemy1/down/enemy1_down1.png"));
    setScale(GameSetting::EnemyScale);
    int max=GameSetting::SceneWidth- sceneBoundingRect().width()*GameSetting::EnemyScale;
    int randomNumber=QRandomGenerator::global()->bounded(1,max);
    setPos(randomNumber,0);
    startTimer(GameSetting::EnemyTimer);
}


void Enemy::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event);
    QList<QGraphicsItem *> itemList=collidingItems();
    for(auto item:itemList){
        if(typeid(*item)==typeid(Player)){
            //健康值需要decrease
            Health::getInstance().decrease();
            scene()->removeItem(this);
            delete this;
            return;
        }
        if(typeid(*item)==typeid(Bullet)){
            //需要加分
            Score::getInstance().increase();
            scene()->removeItem(item);
            scene()->removeItem(this);
            delete item;
            delete this;
            return;
        }
    }


    setPos(x(),y()+GameSetting::Enemyspeed);
    if(y()>GameSetting::SceneHeight){ //析构掉越界敌机
        scene()->removeItem(this);
        delete this;
    }
}
